package com.tucanstubs.android_wheresmystuff.activities;

import java.util.List;
import com.tucanstubs.android_wheresmystuff.R;
import com.tucanstubs.android_wheresmystuff.database.UserHelper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;


/**
 * This class handles the Screen that displays the users.
 *  It retrieves the list of users from the database and displays all of them
 *  on the screen. It and moves to the UserInfoScreen when the user selects
 *  a particular user
 * @author team 37
 * @version 2.0 4/9/2013
 *
 */
public class UserListActivity extends Activity {
	
	private List<String> userList;
	private final UserListActivity that = this;
	private String userName; 
	
	/**
	 * Creates the activity. Initializes all GUI Components 
	 * and retrieves information form the bundle.
	 * The activity receives a bundle with the key Tags.USERNAME which
	 * maps the to user name of the currently logged in user.
	 * @param SavedInstanceState not used
	 */
	public void onCreate(Bundle SavedInstanceState){
		super.onCreate(SavedInstanceState);
		setContentView(R.layout.users_list_screen);
		Bundle bundle=getIntent().getExtras();
		userName = bundle.getString(Tags.USERNAME);
		Log.i(Tags.USERLISTACTIVITY, UserHelper.getUserNames(this).toString());
		
		//arrayList of users
		userList = UserHelper.getUserNames(this);
	
		//creates the list view
		ListView userListView = (ListView) findViewById(R.id.Users_List);
		userListView.setOnItemClickListener(new OnItemClickListener() {
			/**
			 * Method which transfers control over to the user info screen when that user is selected
			 * 
			 */
			public void onItemClick(AdapterView<?> parent, View v, int pos,
					long id) {
				
				Intent nextIntent = new Intent(that, UserInfoActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString(Tags.SELECTEDUSER,userList.get(pos));
				bundle.putString(Tags.USERNAME, userName);
				nextIntent.putExtras(bundle);
				startActivity(nextIntent);
				Log.i(Tags.USERLISTACTIVITY, "You pressed " + pos);
			}
			
		});
		
		//displays the users
		ListAdapter adapter = new ArrayAdapter<String>(this,
				  android.R.layout.simple_list_item_1, android.R.id.text1, userList);
		userListView.setAdapter(adapter);
	}
}
